package com.fy.market.persistent.respository.activity;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fy.market.activity.model.entity.RaffleActivityAccountMonthEntity;
import com.fy.market.activity.repository.IRaffleActivityAccountMonthRepository;
import com.fy.market.persistent.mapper.RaffleActivityAccountMonthMapper;
import com.fy.market.persistent.po.RaffleActivityAccountMonth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 抽奖活动账户表-月次数 仓储实现
 * @author fwq
 * @date 2025-10-11
 */
@Slf4j
@Repository
public class RaffleActivityAccountMonthRepository implements IRaffleActivityAccountMonthRepository {

    @Resource
    private RaffleActivityAccountMonthMapper raffleActivityAccountMonthMapper;

    @Override
    public RaffleActivityAccountMonthEntity queryRaffleActivityAccountMonthByUserIdAndActivityId(Integer userId, Integer activityId, String month) {
        LambdaQueryWrapper<RaffleActivityAccountMonth> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RaffleActivityAccountMonth::getUserId, userId)
                .eq(RaffleActivityAccountMonth::getActivityId, activityId)
                .eq(RaffleActivityAccountMonth::getMonth, month);

        RaffleActivityAccountMonth raffleActivityAccountMonth = raffleActivityAccountMonthMapper.selectOne(queryWrapper);
        if (raffleActivityAccountMonth == null) {
            return null;
        }

        return RaffleActivityAccountMonthEntity.builder()
                .id(raffleActivityAccountMonth.getId())
                .userId(raffleActivityAccountMonth.getUserId())
                .activityId(raffleActivityAccountMonth.getActivityId())
                .month(raffleActivityAccountMonth.getMonth())
                .monthCount(raffleActivityAccountMonth.getMonthCount())
                .monthCountSurplus(raffleActivityAccountMonth.getMonthCountSurplus())
                .createTime(raffleActivityAccountMonth.getCreateTime())
                .updateTime(raffleActivityAccountMonth.getUpdateTime())
                .build();
    }

    @Override
    public boolean decrRaffleActivityAccountMonthCount(Integer userId, Integer activityId, String month) {
        LambdaUpdateWrapper<RaffleActivityAccountMonth> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RaffleActivityAccountMonth::getUserId, userId)
                .eq(RaffleActivityAccountMonth::getActivityId, activityId)
                .eq(RaffleActivityAccountMonth::getMonth, month)
                .gt(RaffleActivityAccountMonth::getMonthCountSurplus, 0)
                .setSql("month_count_surplus = month_count_surplus - 1");

        int updateCount = raffleActivityAccountMonthMapper.update(null, updateWrapper);
        return updateCount > 0;
    }

    @Override
    public void createRaffleActivityAccountMonth(RaffleActivityAccountMonthEntity raffleActivityAccountMonthEntity) {
        RaffleActivityAccountMonth raffleActivityAccountMonth = new RaffleActivityAccountMonth();
        raffleActivityAccountMonth.setUserId(raffleActivityAccountMonthEntity.getUserId());
        raffleActivityAccountMonth.setActivityId(raffleActivityAccountMonthEntity.getActivityId());
        raffleActivityAccountMonth.setMonth(raffleActivityAccountMonthEntity.getMonth());
        raffleActivityAccountMonth.setMonthCount(raffleActivityAccountMonthEntity.getMonthCount());
        raffleActivityAccountMonth.setMonthCountSurplus(raffleActivityAccountMonthEntity.getMonthCountSurplus());
        raffleActivityAccountMonth.setCreateTime(new Date());
        raffleActivityAccountMonth.setUpdateTime(new Date());

        raffleActivityAccountMonthMapper.insert(raffleActivityAccountMonth);
    }

    @Override
    public void updateRaffleActivityAccountMonth(RaffleActivityAccountMonthEntity raffleActivityAccountMonthEntity) {
        RaffleActivityAccountMonth raffleActivityAccountMonth = new RaffleActivityAccountMonth();
        raffleActivityAccountMonth.setId(raffleActivityAccountMonthEntity.getId());
        raffleActivityAccountMonth.setUserId(raffleActivityAccountMonthEntity.getUserId());
        raffleActivityAccountMonth.setActivityId(raffleActivityAccountMonthEntity.getActivityId());
        raffleActivityAccountMonth.setMonth(raffleActivityAccountMonthEntity.getMonth());
        raffleActivityAccountMonth.setMonthCount(raffleActivityAccountMonthEntity.getMonthCount());
        raffleActivityAccountMonth.setMonthCountSurplus(raffleActivityAccountMonthEntity.getMonthCountSurplus());
        raffleActivityAccountMonth.setUpdateTime(new Date());

        raffleActivityAccountMonthMapper.updateById(raffleActivityAccountMonth);
    }
}
